#! /bin/bash
#版本信息
build_date="20200307"
build_version="v1.0.0"
#云崽路径
yunzai_path="/root/Yunzai-Bot"
#当前时间 格式：2020-03-12 14:36:31
NOW_DATE=$(date "+%Y-%m-%d %H:%M:%S")


#当前时间
echo "=========== $(date) ==========="

#定义字体颜色
color_black_start="\033[30m"
color_red_start="\033[31m"
color_green_start="\033[32m"
color_yellow_start="\033[33m"
color_blue_start="\033[34m"
color_purple_start="\033[35m"
color_sky_blue_start="\033[36m"
color_white_start="\033[37m"
color_end="\033[0m"

#提示信息级别定义
message_info_tag="${color_sky_blue_start}[Info]    ${NOW_DATE} ${color_end}"
message_warning_tag="${color_yellow_start}[Warning] ${NOW_DATE} ${color_end}"
message_error_tag="${color_red_start}[Error]   ${NOW_DATE} ${color_end}"
message_success_tag="${color_green_start}[Success] ${NOW_DATE} ${color_end}"
message_fail_tag="${color_red_start}[Failed]  ${NOW_DATE} ${color_end}"
#读取qq管理员序列
function read_array() {
    flag=0
    cat $1 | while read LINE
    do 
        if [ $flag == 0 ];then
            if [ "$(echo $LINE | grep "masterQQ:")" != "" ];then
                flag=1
                continue
            fi
        fi
        if [ $flag == 1 ];then
            if [ "$(echo $LINE | grep -E "^- ")" != "" ];then
                   echo "$LINE" | awk -F " " '{print $2}'
                continue
            else
                break
            fi
        fi
    done
}
#初始化云崽
#修改\添加管理员账号


#查看当前配置(主)
function main_show_conf(){
    if [ -d $yunzai_path/data ]; then
        echo -e  "${color_sky_blue_start}\n =========== 查看当前配置 ==========="
        echo -e  "${color_sky_blue_start}\n 管理员QQ列表："
        echo -e  "${color_sky_blue_start}`read_array $yunzai_path/config/config/other.yaml` "
        echo -e  "${color_sky_blue_start}\n 当前机器人账号："
        echo -e  "${color_sky_blue_start} QQ:`sed '/^qq:/!d;s/.*://' $yunzai_path/config/config/qq.yaml` "
        echo -e  "${color_sky_blue_start} 密码:`sed '/^pwd:/!d;s/.*://' $yunzai_path/config/config/qq.yaml` "
        sleep 5
        main_menu
    else
        echo -e  "${message_warning_tag} 云崽没有初始化，请初始化再查看！ "
        sleep 1
        main_menu
    fi


}
#修改\添加管理员账号（主）
function Yunzai_admin_set() {
    clear
    echo -e "${color_blue_start}选择需要执行的操作"
    echo -e "\n 1.修改管理员 \n 2.添加管理员 \n 3.删除管理员   \n"
    read -p "请输入你的选择（输入数字）:" set_admin
    if [[ "${set_admin}" == "1" ]]; then
        if [ -f $yunzai_path/config/config/other.yaml ]; then
             clear
            echo -e  "当前管理员账号列表："
            echo -e  "${color_sky_blue_start}`read_array $yunzai_path/config/config/other.yaml`"
            echo
            read -p "请输入旧的管理员QQ:" old_QQmaster
            read -p "请添加新的管理员QQ:" QQmaster
            check_qq=`awk -F " " '{print $2}' $yunzai_path/config/config/other.yaml |grep $old_QQmaster `
            echo $check_qq
            if [ "$old_QQmaster" = "$check_qq" ]; then
                clear
                sed -i "s/$check_qq/$QQmaster/g" $yunzai_path/config/config/other.yaml
                echo "当前管理员账号列表："
                echo -e  "${color_sky_blue_start}`read_array $yunzai_path/config/config/other.yaml`"
            else
                echo -e "$message_warning_tag修改失败，管理员账号不存在"
            fi
        else
            echo -e "${message_error_tag}云崽没有初始化，请初始化再操作"
        fi
        sleep 2
        main_menu
    elif [[ "${set_admin}" == "2" ]]; then
        if [ -f $yunzai_path/config/config/other.yaml ]; then
            clear
            echo -e  "当前管理员账号列表："
            echo -e  "${color_sky_blue_start}`read_array $yunzai_path/config/config/other.yaml`"
            echo
            read -p "请添加新的管理员QQ:" QQmaster
            check_qq=`awk -F " " '{print $2}' $yunzai_path/config/config/other.yaml |grep $QQmaster `
            echo $check_qq
            if [ "$QQmaster" != "$check_qq" ]; then
                clear
                sed -i "7i \  \-\ $QQmaster" $yunzai_path/config/config/other.yaml
                echo "当前管理员账号列表："
                echo -e  "${color_sky_blue_start}`read_array $yunzai_path/config/config/other.yaml`"
            else
                echo -e "$message_warning_tag管理员账号存在,请勿重复添加"
            fi
        else
            echo -e "${message_error_tag}云崽没有初始化，请初始化再操作"
        fi
        sleep 2
        main_menu
    elif [[ "${set_admin}" == "3" ]];then
        if [ -f $yunzai_path/config/config/other.yaml ]; then
            clear
            echo -e  "当前管理员账号列表："
            echo -e  "${color_sky_blue_start}`read_array $yunzai_path/config/config/other.yaml`"
            echo
            read -p "请输入删除管理员QQ:" QQmaster
            check_qq=`awk -F " " '{print $2}' $yunzai_path/config/config/other.yaml |grep $QQmaster `
            echo $check_qq
            if [ "$QQmaster" = "$check_qq" ]; then
              num_id=`grep -n "$QQmaster" $yunzai_path/config/config/other.yaml | cut -f1 -d:`
              sed -i "$num_id"d $yunzai_path/config/config/other.yaml
            else
              echo -e "$message_warning_tag管理员账号不存在"
            fi
        else
            echo -e "${message_error_tag}云崽没有初始化，请初始化再操作"
        fi
        sleep 2
        main_menu
    else
        main_menu
    fi
    
echo "$color_white_star"
}
#随机数
function rand(){
    min=$1
    max=$(($2-$min+1))
    num=$(($RANDOM+1000000000)) 
    echo $(($num%$max+$min))
}

function set_conf(){
    #品牌
    cd /root/
    sed -i  's!\("product": "\).*!\1'"$Brand"'",!g' device.json
    sed -i  's!\("device": "\).*!\1'"$Brand"'",!g' device.json
    sed -i  's!\("board": "\).*!\1'"$Brand"'",!g' device.json
    #型号
    sed -i  's!\("model": "\).*!\1'"$Model"'",!g' device.json
    #IMEI
    sed -i  's!\("imei": "\).*!\1'"$IMEI"'",!g' device.json
    #MAC
    sed -i  's!\("mac_address": "\).*!\1'"$MAC_ADDR"'",!g' device.json
    #IP
    sed -i  's!\("ip_address": "\).*!\1'"$IP_ADDR"'",!g' device.json
    #BOOT_ID
    sed -i  's!\("boot_id": "\).*!\1'"$BOOT_ID"'",!g' device.json
    #系统版本
    sed -i  's!\("proc_version": "\).*!\1'"$Sys_Version"'",!g' device.json
    #A_id
    sed -i  's!\("android_id": "\).*!\1'"$A_id"'",!g' device.json
    #INC
    sed -i  's!\("incremental": "\).*!\1'"$INC"'"!g' device.json
}



function device-set() {
    #局部变量
    read -p "请添加更改设备文件QQ号:" QQ_ID
    #系统版本
    Sys_Version=`awk '{print $1,$2,$3}' /proc/version `
    #mac地址
    MAC_ADDR=`echo $RANDOM | md5sum | sed 's/../&:/g' | cut -c1-17 `
    #ip地址
    IP_ADDR=` awk '{print $1}' /etc/hosts |grep 172* `
    #boot_id
    BOOT_ID=`cat /proc/sys/kernel/random/uuid`
    #随机设备
    device_run_function=$(rand 1 4)
        #设备随机生成、三星、小米、OPPO、华为
    if [[ "${device_run_function}" == "1" ]]; then
        Brand="SAMSUNG"
        Model="Galaxy S10 Exynos"
        IMEI="35480910$(rand 1000000 9999999)"
        #incremental
        INC=$(rand 1600000000 1999999999)
        #android_id
        A_id="d0d2ae8884b$(rand 10000 99999)"
        echo "品牌:$Brand"
        echo "型号：$Model"
        echo "imei:$IMEI"
        echo  "MAC地址:$MAC_ADDR"
        echo "ip地址：$IP_ADDR"
        echo "boot_id:$BOOT_ID"
        echo "系统版本：$Sys_Version"
        set_conf
    elif [[ "${device_run_function}" == "2" ]]; then
        Brand="XIAOMI"
        Model="Redmi Note 8"
        IMEI="86436505$(rand 1000000 9999999)"
        #incremental
        INC=$(rand 1700000000 1999999999)
        #android_id
        A_id="d0d2ae6582b$(rand 10000 99999)"
        echo "品牌:$Brand"
        echo "型号：$Model"
        echo "imei:$IMEI"
        echo  "MAC地址:$MAC_ADDR"
        echo "ip地址：$IP_ADDR"
        echo "boot_id:$BOOT_ID"
        echo "系统版本：$Sys_Version"
        set_conf
    elif [[ "${device_run_function}" == "3" ]]; then
        Brand="OPPO"
        Model="A3s"
        IMEI="86935103$(rand 1000000 9999999)"
        #incremental
        INC=$(rand 1600000000 1999999999)
        #android_id
        A_id="d0d2ae995sb$(rand 10000 99999)"
        echo "品牌:$Brand"
        echo "型号：$Model"
        echo "imei:$IMEI"
        echo  "MAC地址:$MAC_ADDR"
        echo "ip地址$IP_ADDR"
        echo "boot_id:$BOOT_ID"
        echo "系统版本：$Sys_Version"
        set_conf
    elif [[ "${device_run_function}" == "4" ]]; then
        Brand="HUAWEI"
        Model="P30 Pro New Edition"
        IMEI="35823445$(rand 1000000 9999999)"
        #incremental
        INC=$(rand 1050000000 1999999999)
        #android_id
        A_id="d0d2ae6851db$(rand 10000 99999)"
        echo "品牌:$Brand"
        echo "型号：$Model"
        echo "imei:$IMEI"
        echo  "MAC地址:$MAC_ADDR"
        echo "ip地址：$IP_ADDR"
        echo "boot_id:$BOOT_ID"
        echo "系统版本：$Sys_Version"
        set_conf

    fi

    if [ -d $yunzai_path/data ]; then
        echo "生成设备文件"
        if [ ! -d $yunzai_path/data/$QQ_ID ]; then
            mkdir $yunzai_path/data/$QQ_ID
        fi
        cp device.json $yunzai_path/data/$QQ_ID/"device-$QQ_ID.json"
    else
            echo -e "$message_error_tag 云崽没有初始化，无法替换文件"
    fi
    sleep 2
    main_menu
}
#菜单（主）
function main_menu() {
    clear
    echo -e "${color_blue_start}选择需要执行的功能"
    echo -e "\n 1.初始化\掉线登录 \n 2.修改机器人QQ \n 3.修改\添加管理员账号  \n 4.开启机器人 \n 5.查看当前配置 \n 6.重新生成设备文件 \n 7.查看当前日志  \n 0.退出 \n"
    read -p "请输入你的选择（输入数字）:" run_function
    if [[ "${run_function}" == "1" ]]; then
            cd $yunzai_path
            node app
        elif [[ "${run_function}" == "2" ]]; then
            cd $yunzai_path
            npm run login
        elif [[ "${run_function}" == "3" ]]; then
            Yunzai_admin_set
        elif [[ "${run_function}" == "4" ]]; then
            cd $yunzai_path
            npm run start
        elif [[ "${run_function}" == "5" ]]; then
            main_show_conf
        elif [[ "${run_function}" == "6" ]]; then
            device-set
        elif [[ "${run_function}" == "7" ]]; then
            cd $yunzai_path
            npm run log
        elif [[ "${run_function}" == "0" ]]; then
        start=0
        else
            main_menu
    fi
}
main_menu